<script setup>
import { useUserStore } from '@/store'
import { storeToRefs } from 'pinia'
import { useRouter, useRoute } from 'vue-router'
import { ref, watch } from 'vue'
import { reg, Login } from '@/api/login.js'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
const userStore = useUserStore();
const router = useRouter()
const route = useRoute();
const isRegister = ref(true);
const form = ref(null);
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
});
const rules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'change'
    },
    {
      pattern: /^\S{6,15}$/,
      message: '请输入正确格式的密码',
      trigger: ''
    }
  ],
  repassword: [
    {
      required: true,
      message: '请再次输入密码',
      trigger: 'change'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ]
});
const register = async () => {
  await form.value.validate();
  // 成功就继续走  不成功会自动校验的
  const res = await reg(formModel.value);
  console.log(res);
  ElMessage.success(res.data.message);
  isRegister.value = false;
};
const login = async () => {
  await form.value.validate();
  const res = await Login(formModel.value);
  console.log(res);
  userStore.setToken(res.data.token);
  ElMessage.success(res.data.message);
  router.push('/');
};
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    repassword: ''
  };
});
</script>
<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <el-form
        class="form"
        :model="formModel"
        size="large"
        :rules="rules"
        autocomplete="off"
        v-if="isRegister"
        ref="form"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            v-model="formModel.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            type="password"
            v-model="formModel.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            :prefix-icon="Lock"
            v-model="formModel.repassword"
            type="password"
            placeholder="请再次输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            auto-insert-space
            class="button"
            type="primary"
            @click="register"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回</el-link
          >
        </el-form-item>
      </el-form>
      <el-form
        ref="form"
        size="large"
        class="form"
        :model="formModel"
        :rules="rules"
        autocomplete="off"
        v-else
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            v-model="formModel.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            type="password"
            v-model="formModel.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            auto-insert-space
            type="primary"
            class="button"
            @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →</el-link
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background: url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
      url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
